package com.tiger.novelAvinfo;

import java.util.List;
import java.util.Map;

import com.tiger.novelAvinfo.base.BaseActivity;
import com.tiger.novelAvinfo.base.CommonUtil;
import com.tiger.novelAvinfo.base.DataServer;
import com.tiger.novelAvinfo.base.DataServerFactory;

import android.app.Activity;
import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.view.GestureDetector;
import android.view.MotionEvent;
import android.view.View;
import android.view.Window;
import android.view.GestureDetector.OnGestureListener;
import android.view.View.OnClickListener;
import android.view.animation.AnimationUtils;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.ViewFlipper;

public class LookActivity extends BaseActivity  implements OnGestureListener {
	private ViewFlipper flipper;
	private Map<String,Object> avPerson;
	private DataServerFactory serverData = DataServerFactory.getInstance();
	private List<AVInfo> infos;
	private Bundle bundle;
	private String zhName,enName;
	private TextView avName;
	private int avPosition;
	private String comment;
	private GestureDetector detector;
	private ImageView prePerson,nextPerson;
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        this.requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.look);
        CommonUtil.setAdView(this, this);
        bundle=this.getIntent().getExtras();
        avPosition=bundle.getInt("avNum");
        
        initView();
    }
    private void initView(){
    	detector=new GestureDetector(this);
    	infos=serverData.getInfos();
    	
    	flipper=(ViewFlipper)this.findViewById(R.id.ImageViewFlipper);
    	avName=(TextView)this.findViewById(R.id.avName);
//    	imageShow=(ImageView)this.findViewById(R.id.iamgeShow);
    	prePerson=(ImageView)this.findViewById(R.id.prePerson);
    	nextPerson=(ImageView)this.findViewById(R.id.nextPerson);
    	prePerson.setOnClickListener(new OnClickListener(){
			@Override
			public void onClick(View arg0) {
				if(avPosition==0){
					avPosition=infos.size()-1;
				}else{
				avPosition--;
				}
				setData();
			}
    	});
    	nextPerson.setOnClickListener(new OnClickListener(){
			@Override
			public void onClick(View arg0) {
				if((avPosition+1)==infos.size()){
					avPosition=0;
				}else{
				avPosition++;
				}
				setData();
			}
    	});
    	flipper.setOnClickListener(new OnClickListener(){
			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				Intent intent=new Intent(LookActivity.this,DetailActivity.class);
				Bundle bundle=new Bundle();
				bundle.putString("comment", comment);
				bundle.putString("zhName", zhName);
				intent.putExtras(bundle);
				LookActivity.this.startActivity(intent);
			}
    	});
    	setData();
    }
    private void initAvPerson(){
    	avName.setText(zhName);
    	flipper.removeAllViews();
    	Drawable[] picDraws=(Drawable[])avPerson.get(CommonUtil.picDrawablesKey);
    	for(int i=0;i<picDraws.length;i++){
    		ImageView view=new ImageView(this);
    		view.setBackgroundDrawable(picDraws[i]);
    		flipper.addView(view);
    	}
    	comment=(String)avPerson.get(CommonUtil.commentKey);
    }
    private void setData(){
    	enName=infos.get(avPosition).getEnglish_Name();
    	zhName=infos.get(avPosition).getChinese_Name();
    	avPerson=CommonUtil.getAVPerson(this, enName, zhName);
    	initAvPerson();
    }
	@Override
	public boolean onDown(MotionEvent arg0) {
		// TODO Auto-generated method stub
		return false;
	}
	@Override
	public boolean onTouchEvent(MotionEvent event) {
		return this.detector.onTouchEvent(event);
	}
	@Override
	public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
			float velocityY) {
		// TODO Auto-generated method stub
		if (e1.getX() - e2.getX() > 120) {
			this.flipper.setInAnimation(AnimationUtils.loadAnimation(this,
					R.anim.push_left_in));
			this.flipper.setOutAnimation(AnimationUtils.loadAnimation(this,
					R.anim.push_left_out));
			this.flipper.showNext();
			return true;
		} else if (e1.getX() - e2.getX() < -120) {
			this.flipper.setInAnimation(AnimationUtils.loadAnimation(this,
					R.anim.push_right_in));
			this.flipper.setOutAnimation(AnimationUtils.loadAnimation(this,
					R.anim.push_right_out));
			this.flipper.showPrevious();
			return true;
		}
		return false;
	}
	@Override
	public void onLongPress(MotionEvent arg0) {
		// TODO Auto-generated method stub
		
	}
	@Override
	public boolean onScroll(MotionEvent arg0, MotionEvent arg1, float arg2,
			float arg3) {
		// TODO Auto-generated method stub
		return false;
	}
	@Override
	public void onShowPress(MotionEvent arg0) {
		// TODO Auto-generated method stub
		
	}
	@Override
	public boolean onSingleTapUp(MotionEvent arg0) {
		// TODO Auto-generated method stub
		return false;
	}
}